package com.xbongbong.paas.service.workflow.setting.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.enums.ComboTypeEnum;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.help.ProFormHelp;
import com.xbongbong.paas.pojo.workflow.settings.WorkflowGetPojo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.util.ItemUtil;
import com.xbongbong.paas.util.WorkflowNodeUtil;
import com.xbongbong.pro.enums.workflow.WorkflowValueTypeEnum;
import com.xbongbong.workflow.pojo.nodes.WorkflowConditionPojo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/06/01 20:05
 */
@Service("conditionNodeComboServiceImpl")
public class ConditionNodeComboServiceImpl extends AbstractConditionNodeFieldService {

    @Resource
    private ProFormHelp proFormHelp;

    @Override
    public List<Integer> type() {
        return Arrays.asList(FieldTypeEnum.COMBO.getType(), FieldTypeEnum.COMBO_CHECK.getType(),
                FieldTypeEnum.RADIO_GROUP.getType(), FieldTypeEnum.CHECKBOX_GROUP.getType());
    }


    @Override
    public String conditionField(WorkflowConditionPojo workflowConditionPojo, WorkflowGetPojo workflowGetPojo) throws XbbException {
        String value = "";
        Map<String, FieldAttrEntity> explainMap = WorkflowNodeUtil.getExplainMap(workflowGetPojo, workflowConditionPojo.getBusinessType(), workflowConditionPojo.getFormId());
        FieldAttrEntity fieldAttrEntity = explainMap.get(workflowConditionPojo.getAttr());
        List<ItemPoJo> itemList = proFormHelp.getItem(fieldAttrEntity, workflowConditionPojo.getBusinessType(), workflowGetPojo.getCorpid(), workflowConditionPojo.getFormId());
        fieldAttrEntity.setItems(itemList);
        String attrName = fieldAttrEntity.getAttrName();
        String symbol = ConditionEnum.getMemoBySymbo(workflowConditionPojo.getSymbol());
        if (Objects.equals(WorkflowValueTypeEnum.FIXED_VALUE.getType(), workflowConditionPojo.getValueType())) {

            if (CollectionUtils.isNotEmpty(workflowConditionPojo.getValue())) {
                List<Object> valueList = new ArrayList<>();
                if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), fieldAttrEntity.getComboType())) {
                    workflowConditionPojo.getValue().forEach(item->{
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put(workflowConditionPojo.getAttr(), item);
                        valueList.add(ItemUtil.getCustomItemList(fieldAttrEntity, jsonObject));
                    });
                } else {
                    workflowConditionPojo.getValue().forEach(item->{
                        valueList.add(item);
                    });
                }
                value = attrName + symbol + StringUtils.join(valueList, "、");
            } else {
                // 为空、不为空
                value = attrName + symbol;
            }
        } else {
            value = getDynamicValue(attrName, symbol, workflowGetPojo, workflowConditionPojo);
        }
        return value;
    }
}
